package com.java2007.hotel.controller;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
 * 处理订单请求
 * @author csl
 * @version V1.0
 * @date 2021/01/23
 **/
@WebServlet("/hotel/orderServlet")
public class OrderServlet extends  BaseServlet{
    private OrderService orderService = (OrderService) BeanFactory.getBean(Constant.ORDER_SERVICE_IMPL);

    /**
     *查询所有订单
     * @param req
     * @param resp
     * @return
     */
    public String selectAll(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        List<Order> orders = null;
        try {
            orders = orderService.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            return "upIndex";
        }
        session.setAttribute("orders", orders);
        return "redirect:" + req.getContextPath() + "/hotel/backend/detail/order/orderList.jsp";
    }

    /**
     * 改结账状态
     * @param req
     * @param resp
     * @return
     */
    public String overOrder(HttpServletRequest req, HttpServletResponse resp){
        String tempId = req.getParameter("id");
        String tempStatus = req.getParameter("status");
        byte status = 0;
        int id = Integer.parseInt(tempId);
        try {
            orderService.updateOrderStatus(status,id);
        } catch (SQLException e) {
            e.printStackTrace();
            return "upIndex";
        }
        return "redirect:" + req.getContextPath() + "/hotel/orderServlet?method=selectAll";

    }


    /**
     * 删除订单
     * @param req
     * @param resp
     * @return
     */
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String tempId = req.getParameter("id");
        int id = Integer.parseInt(tempId);
        try {
            int delete = orderService.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
            return "upIndex";
        }

        return "redirect:" + req.getContextPath() + "/hotel/orderServlet?method=selectAll";

    }
}

